package cn.shenlong.portal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Page;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.mapper.IconMapper;
import cn.shenlong.portal.service.IconService;

@Service("IconService")
public class IconServiceImpl implements IconService {

	@Autowired
	private IconMapper IconMapper;
	
	@Override
	@Transactional
	public void save(Icon icon,long iconId) {
		IconMapper.saveEntity(icon);
	}

	@Override
	public Pagination findIcon(Icon icon, Map<String, Object> otherParm,Integer pageSize, Integer pageNo) {
		Pagination p = new Pagination();
		List<Icon> contentList= IconMapper.findPageListByCriteria(initiconParm(icon,otherParm), initRowBounds(pageSize, pageNo, p));
		Long count = IconMapper.findCountByCriteria(initiconParm(icon,otherParm));
		p.setList(contentList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	@Override
	public Page<Icon> findPageByParm(Map<String, Object> otherParm, Integer pageNo, Integer pageSize) {
		Page<Icon> p = new Page<Icon>();
		List<Icon> eventList = IconMapper.findPageListByCriteria(otherParm, initRowBounds(pageSize, pageNo, p));
		Long count = IconMapper.findCountByCriteria(otherParm);
		p.setList(eventList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}
	
	private Map<String,Object> initiconParm(Icon icon,Map<String,Object> otherParm){
		Map<String,Object> parm = new HashMap<String, Object>();
		if(otherParm!=null){
			parm.putAll(otherParm);
		}
		if(icon==null){
			return parm;
		}else{
			parm.put("iconId", icon.getIconId());
			parm.put("title", icon.getTitle());
		    parm.put("description", icon.getDescription());
		    parm.put("imgUrl", icon.getImgUrl());
			return parm;
		}

	}
    private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Pagination p) {
        RowBounds rowBounds = null;
        if (pageSize == null || pageNo == null) {
            rowBounds = new RowBounds();
        } else {
            p.setPageNo(pageNo);
            p.setPageSize(pageSize);
            rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
        }
        return rowBounds;
    }

	@Override
	public Icon findIconByIconId(long iconId) {
		return IconMapper.findByPK(iconId);
	}

	@Override
	public Icon updateIcon(Icon icon) {
		 int i = IconMapper.updateEntity(icon);
		 if(i<0) return null;
		 return IconMapper.findByPK(icon.getIconId());
	}

	@Override
	public void deleteIcon(Long[] iconId) {
		IconMapper.deleteByPKArray(iconId);
	}

	@Override
	public void deleteByIconId(List<Long> iconId) {
		IconMapper.deleteByPKArray((Long[])iconId.toArray());
	}
	
	public void save(Icon icon){
		IconMapper.saveEntity(icon);
	}
}
